<template>
	<view class="recharge">
		<view class="mymd flex-cneter u-m-t-100">
			<view class="price u-font-bold u-font-50">
				{{credit}}
			</view>
			<view class="u-font-26" style="color: #999999;">
				当前魔豆
			</view>
		</view>
		<u-line style="margin: 30rpx 0;"></u-line>
		<view class="">充值金额</view>
		<view class="flex rechargelist" >
			<view @click="chooseprice(i.id)" :class="current==i.id?'actve':''" class="rechargelist_item flex u-m-t-20" v-for="(i,index) in list">
				<view class="u-m-t-30">{{i.givenum}}魔豆</view>
				<view class="u-font-26 u-m-t-30" style="color: #999999;">{{i.price}}元</view>
			</view>
		</view>
		<view class="u-m-t-30">支付方式</view>
		<view class="pay_type">
			
			<!-- #ifdef APP -->
			<view class="item" @click="paytype=0">
				<view class="l">
					<image class="img" src="/static/index/zfb.png" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<image v-if="paytype === 0" class="selectImg" src="/static/selected.png" mode=""></image>
				<image v-else class="selectImg" src="/static/index/select.png" mode=""></image>
			</view>
			<view class="item" @click="paytype=1">
				<view class="l">
					<image class="img" src="/static/index/wx.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<image v-if="paytype === 1" class="selectImg" src="/static/selected.png" mode=""></image>
				<image v-else class="selectImg" src="/static/index/select.png" mode=""></image>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP -->
			<view class="item" @click="paytype=2">
				<view class="l">
					<image class="img" src="/static/index/wx.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<image v-if="paytype === 2" class="selectImg" src="/static/selected.png" mode=""></image>
				<image v-else class="selectImg" src="/static/index/select.png" mode=""></image>
			</view>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<view class="item" @click="paytype=3">
				<view class="l">
					<image class="img" src="/static/index/wx.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<image v-if="paytype === 3" class="selectImg" src="/static/selected.png" mode=""></image>
				<image v-else class="selectImg" src="/static/index/select.png" mode=""></image>
			</view>
			<!-- #endif -->
			
		</view>
		<view class="rechargebtn" @click="chongzhi">
			立即充值
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				current:0,
				paytype:'',
				list:[],
				credit: ''
			};
		},
		onLoad(option) {
			// #ifdef H5
			this.paytype = 3
			// #endif
			// #ifdef APP
			this.paytype = 0
			// #endif
			this.init()
			this.getInfo()
		},
		methods:{
			getInfo(){
				this.$api.post('/creditlist',{page:this.page}).then(res=>{
					this.credit = res.data.credit
				})
			},
			init(){
				
				this.$api.post('/creditorder',{}).then(res=>{
					this.list = res.data
				})
			},
			chongzhi(){
				if(this.current<=0){
					this.$toast('请选择套餐~')
					return
				}
				if(this.paytype<0){
					this.$toast('请选择支付方式~')
					return
				}
				uni.showLoading({
					title:'支付中'
				})
				this.$api.post('/creditorder',{type:'recharge',id:this.current,paytype:this.paytype}).then(res=>{
					if(res.code == 1){
						let data = {
							pay: res.data,
							paytype: this.paytype,
						}
						
						// this.$toast(res.msg)
						this.$pay(data).then(()=>{
							this.getInfo()
							uni.hideLoading()
						}).catch(e => {
							uni.hideLoading();
						})
					}
				})
			},
			chooseprice(id){
				this.current = id
			}
		}
	}
</script>

<style lang="scss">
page{
	background-color: #fff;
}
.recharge{
	padding: 30rpx;
	box-sizing: border-box;
	.mymd{
		flex-direction: column;
		.price{
			color: #00CE9B;
		}
	}
	.rechargelist{
		align-items: center;
		flex-wrap: wrap;
		.rechargelist_item{
			margin: 20rpx 10rpx;
			align-items: center;
			flex-direction: column;
			background: #F8F8FA;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			opacity: 1;
			width: 30%;
			padding: 30rpx 0;
		}
		.actve{
			border: 2rpx solid #00CE9B;
			background: rgba(0,206,155,0.08);
			color: #00CE9B;
		}
	}
}
.pay_type{
	.item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 686rpx;
		height: 140rpx;
		background: #F8F8FA;
		border-radius: 20rpx;
		padding: 52rpx;
		box-sizing: border-box;
		margin: 30rpx auto 0;
		.l{
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333333;
			.img{
				width: 42rpx;
				height: 38rpx;
				margin-right: 20rpx;
			}
		}
		.selectImg{
			width: 32rpx;
			height: 32rpx;
		}
	}
}
.rechargebtn{
	position: absolute;
	width: 90vw;
	bottom:50rpx;
	padding: 30rpx;
	color: #fff;
	left: 5%;
	box-sizing: border-box;
	background-color: #00CE9B;
	text-align: center;
	border-radius: 20rpx;
}
</style>
